package com.d1711.zhanggm.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.d1711.zhanggm.springboot.entity.User;
import com.d1711.zhanggm.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张国明
 * @since 2020-05-27
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService iUserService;

    @PostMapping("login")
    public Map<String,Object> login(@RequestBody User user){
        Map<String,Object> result = new HashMap<>();
        result.put("result",false);
        //验证用户名和密码是否为空

        //查询一条用户信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",user.getUsername());
        User userInfo = iUserService.getOne(queryWrapper);
        //验证密码
        if(userInfo==null || !userInfo.getPassword().equals(user.getPassword())){
            result.put("msg","用户或密码错误");
            return result;
        }
        //验证通过返回用户信息
        result.put("result",true);
        result.put("userInfo",userInfo);
        return result;
    }

}
